package com.android.basewars;


import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class RankingActivity extends Activity {
	
	protected JSONArray results;
	int userID;
	ListView lv;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rankingview);
		
		
		lv = (ListView)findViewById(R.id.listView1);
		
		Bundle extras = getIntent().getExtras();
	    if(extras !=null)
	    {
	    	userID = extras.getInt("userid"); 
	    	getRanking();
	    }
	    
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.map:
	    	Intent m = new Intent(getApplicationContext(), BaseWarsActivity.class);
	 		m.putExtra("userid", userID);
	 		startActivity(m);
	 		finish();
	        return true;
	    case R.id.rankinglist:
//	    	Intent rl = new Intent(getApplicationContext(), RankingActivity.class);
//	 		rl.putExtra("userid", userID);
//	 		startActivity(rl);
//	 		finish();
	        return true;
	    case R.id.attacklog:
	    	Intent rl = new Intent(getApplicationContext(), AttackLogActivity.class);
	 		rl.putExtra("userid", userID);
	 		startActivity(rl);
	 		finish();
	        return true;
	    case R.id.editaccount:
	    	Intent e = new Intent(getApplicationContext(), EditAccountActivity.class);
	 		e.putExtra("userid", userID);
	 		startActivity(e);
	 		finish();
	        return true;  
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	private void setListViewAdapter() {
		  ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
	        HashMap<String, String> map;
			for(int i=0;i <results.length();i++){
				JSONObject o;
				try {
					o = results.getJSONObject(i);
					
					String username =  o.getString("username");
				 	String credits = o.getString("credits");
					map = new HashMap<String,String>();
					map.put("username", username);
					map.put("credits", credits);
					mylist.add(map);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 
			}


			SimpleAdapter adapterSpreekwoorden = new SimpleAdapter(this, mylist, R.layout.rowlistviewalfbet, new String[] {"username", "credits"}, new int[] {R.id.rowListUsername,R.id.rowListCredits});
			
			lv.setAdapter(adapterSpreekwoorden);
			

			lv.setOnItemClickListener(new ListView.OnItemClickListener() {
			    @Override
			    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
			    	JSONObject user = null;
			    	try {
						user = results.getJSONObject(i);
						
						String naam = user.getString("firstname") + " " + user.getString("suffix") + " " + user.getString("lastname");
				    	String username = user.getString("username");
				    	String credits = user.getString("credits");
				    	String email = user.getString("email");
				        final Dialog dialog = new Dialog(RankingActivity.this);
			    		 
			    		 WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();  
			    		 lp.dimAmount=0.0f;  
			    		 dialog.getWindow().setAttributes(lp);  
			    		 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
			    		 
			    		 TextView tv = new TextView(dialog.getContext());
			    		 tv.setPadding(10, 10, 10, 10);
			    		 tv.setText("Naam:          " + naam + "\n" + 
			    				    "Email:          " + email+ "\n" +
			    				    "Username:  " + username + "\n" +
			    				    "Credits:       " + credits );
			    		 			
			    		 dialog.setTitle("User information.");
			    		 dialog.setContentView(tv);
			    		 //dialog.setContentView(android.);
			    		 
					     
					    dialog.show();
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			    }
			});
	}
	
	
	private void getRanking() {
		Handler handler = new Handler() {
			
			public void handleMessage(Message message) {
				switch (message.what) {
				case HttpConnection.DID_START: {
					break;
				}
				case HttpConnection.DID_SUCCEED: {
					String response = (String) message.obj;
					try {
						JSONObject ranking = new JSONObject(response);
						boolean b = ranking.getBoolean("success");
						if(b){
							results = ranking.getJSONArray("result");
							
							setListViewAdapter();
						
				 		}else{
				 			//geen basisen
						}
					}
					catch (JSONException e) {
						e.printStackTrace();
					}
					break;
				}
				case HttpConnection.DID_ERROR: {
					Exception e = (Exception) message.obj;
					e.printStackTrace();
					break;
				}
				}
			}
			
		};
		new HttpConnection(handler)
				.get("http://frankkie.nl/basewars/api/v4/ranking");
		
	}
	
	
	
	
}



